package com.jingdianjichi.user.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.jingdianjichi.changerecord.annotation.Change;
import com.jingdianjichi.changerecord.entity.Dict;
import com.jingdianjichi.changerecord.strategy.ChangeRecordStrategy;
import com.jingdianjichi.changerecord.strategy.DictStrategy;
import com.jingdianjichi.user.dto.ChangeDTO;
import com.jingdianjichi.user.dto.ChangeObjectDTO;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * 变更记录
 *
 * @author Hu_bx
 * @date 15:01 2023/9/16
 **/
@Component
@RequiredArgsConstructor
@RestController
@RequestMapping("change_record")
@Api(tags = "变更记录controller")
public class ChangeRecordController {

    private final ChangeRecordStrategy changeRecordStrategy;
    private final DictStrategy dictStrategy;

    @PostConstruct
    public void init() {
        dictStrategy.initDict(CollUtil.toList(new Dict().setValue("红色").setCode("red").setParentCode("DICT_COLOUR"),
                new Dict().setValue("绿色").setCode("green").setParentCode("DICT_COLOUR"),
                new Dict().setValue("女人").setCode("woman").setParentCode("DICT_GENDER"),
                new Dict().setValue("男人").setCode("man").setParentCode("DICT_GENDER")
        ));
    }


    @Change(type = Change.Type.QUERY)
    @GetMapping("getInfo/{id}")
    public ChangeDTO getInfo(@PathVariable String id) {
        ChangeDTO changeDTO = new ChangeDTO().setName("张三").setId(id).setAge(36).setBirthday(DateUtil.date());
        ChangeObjectDTO changeObjectDTO = new ChangeObjectDTO()
                .setAge(36)
                .setGender("woman")
                .setId(id + 1)
                .setName("妻子")
                .setColour(CollUtil.join(CollUtil.toList("red", "green"), StrUtil.COMMA));
        changeDTO.setWife(changeObjectDTO);
        ChangeObjectDTO changeObjectDTO2 = new ChangeObjectDTO()
                .setAge(12)
                .setGender("woman")
                .setId(id + 2)
                .setName("女儿")
                .setColour(CollUtil.join(CollUtil.toList("green"), StrUtil.COMMA));
        ChangeObjectDTO changeObjectDTO1 = new ChangeObjectDTO()
                .setAge(14)
                .setGender("man")
                .setId(id + 3)
                .setName("儿子")
                .setColour(CollUtil.join(CollUtil.toList("red"), StrUtil.COMMA));
        changeDTO.setChildList(CollUtil.toList(changeObjectDTO2, changeObjectDTO1));
        return changeDTO;
    }

    /**
     * 更新
     *
     * @param changeDTO changeDTO
     * @return com.jingdianjichi.bean.Result<com.jingdianjichi.user.dto.ChangeDTO>
     * @author Hu_bx
     * @date 15:30 2023/9/16
     **/
    @Change(type = Change.Type.CHANGE)
    @PostMapping("update")
    public ChangeDTO update(@RequestBody ChangeDTO changeDTO) {
        return changeDTO;
    }

    /**
     * 获取变更记录
     *
     * @param id id
     * @return com.jingdianjichi.bean.Result<java.util.List < java.lang.String>>
     * @author Hu_bx
     * @date 15:30 2023/9/16
     **/
    @GetMapping("getDescribe/{id}")
    public List<String> getDescribe(@PathVariable String id) {
        // [
        //  "[修改][名字]由[张三]变为了[张三1],[年龄]由[36]变为了[32],[生日]由[2023-09-16 16:19:43]变为了[2023-09-16 04:19:43]"
        //]
        return changeRecordStrategy.getDescribe(id);
    }


}
